package com.clip.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.clip.entity.SystemMessage;

@Repository
public class SystemMessageDAO extends BasicDao  {
	private static final Log log = LogFactory.getLog(SystemMessageDAO.class);
	//property constants
	public static final String UID = "uid";
	public static final String EVENTID = "eventid";


    
    public void save(SystemMessage transientInstance) {
        log.debug("saving SystemMessage instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(SystemMessage persistentInstance) {
        log.debug("deleting SystemMessage instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
	public void deleteAllByEventid(String eventid) {
        try {
            getSession().createQuery("delete from SystemMessage au where au.eventid='"+eventid+"'").executeUpdate();
        } catch (RuntimeException re) {
            throw re;
        }
    }
	
    public SystemMessage findById( java.lang.Integer id) {
        log.debug("getting SystemMessage instance with id: " + id);
        try {
            SystemMessage instance = (SystemMessage) getSession()
                    .get(SystemMessage.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(SystemMessage instance) {
        log.debug("finding SystemMessage instance by example");
        try {
            List results = getSession()
                    .createCriteria(SystemMessage.class)
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding SystemMessage instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from SystemMessage as model where model." 
         						+ propertyName + "= ? ";
         if(propertyName.equals(this.EVENTID))
        	 queryString+="order by model.replytime";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByUid(Object uid
	) {
		return findByProperty(UID, uid
		);
	}
	
	public List findByEventid(Object eventid
	) {
		return findByProperty(EVENTID, eventid
		);
	}
	
	

	public List findAll() {
		log.debug("finding all SystemMessage instances");
		try {
			String queryString = "from SystemMessage";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public SystemMessage merge(SystemMessage detachedInstance) {
        log.debug("merging SystemMessage instance");
        try {
            SystemMessage result = (SystemMessage) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(SystemMessage instance) {
        log.debug("attaching dirty SystemMessage instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(SystemMessage instance) {
        log.debug("attaching clean SystemMessage instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void excuteUpdateQuery(String query){
    	try {
			getSession().createQuery(query).executeUpdate();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
    }
    
    public List findByQuery(String query){
		return findByQuery(query,null);
    }
    
    public List findByQuery(String query, Object...args) {
		Query queryobj=getSession().createQuery(query);
		if(args!=null&&args.length>0){
			for (int i = 0; i < args.length; i++) {
				queryobj.setParameter(i, args[i]);
			}
		}
		return queryobj.list();
    }
    
    public Object countByQuery(String query){
    	return getSession().createQuery(query).uniqueResult();
    }

	public List<SystemMessage> pagingByQuery(String hql, int currentpage,int pagelength, Object...args) {
		Query query=getSession().createQuery(hql);
		query.setFirstResult(currentpage*pagelength);
		query.setMaxResults(pagelength);
		if(args!=null&&args.length>0){
			for (int i = 0; i < args.length; i++) {
				query.setParameter(i, args[i]);
			}
		}
		return query.list();
	}
}
